
import 'package:syria_sports/data/models/error_model.dart';
import 'package:syria_sports/core/utils/pref_utils.dart';
import 'package:syria_sports/core/utils/show_toast.dart';
import 'package:syria_sports/data/models/data.dart';
import 'package:syria_sports/data/remote/dio_helper.dart';
import 'package:syria_sports/data/apiClient/api_client.dart';
import 'package:syria_sports/data/apiClient/api_keys.dart';

import '../../presentation/offers_page/models/offer.dart';

class OffersController {
  static Future<Data?> getAllOffers() async {
    var res = await DioHelper.getData(
      url: ApiClient.offers,
      bearerToken: PrefUtils.getToken(),
    );
    if (res.statusCode != 200) {
      ErrorModel.fromResponse(res);
      return null;
    }
    Data response = Data.fromJson(res.data);
    return response;
  }

  static Future<Data?> addOffer(Offer offer) async {
    var res = await DioHelper.postData(
      url: ApiClient.offer,
      data: offer.toJson(),
      bearerToken: PrefUtils.getToken(),
    );

    if (res == null) return null;
    if (res.statusCode != 200) {
      ErrorModel.fromResponse(res);
    } else {
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }

  static Future<Data?> getOfferById(int id) async {
    var res = await DioHelper.getData(
      url: '${ApiClient.offer}/$id',
      bearerToken: PrefUtils.getToken(),
    );
    if (res.statusCode != 200) {
      ErrorModel.fromResponse(res);
    } else if (!res.data[ApiKeys.state]) {
      showMessage(res.data[ApiKeys.message]);
    } else {
      Data response = Data.fromJson(res.data);
      return response;
    }
    return null;
  }

  static Future<Data?> deleteById(String id) async {
    var res = await DioHelper.postData(
      url: ApiClient.offerDelete,
      data: {
        'id': id,
      },
      bearerToken: PrefUtils.getToken(),
    );
    if (res == null) return null;
    if (res.statusCode != 200) {
      ErrorModel.fromResponse(res);
    } else {
      showMessage(res.data[ApiKeys.message]);
    }

    return Data.fromJson(res.data);
  }


  static Future<Data?> selectOffer(Offer offer) async {
    var res = await DioHelper.postData(
      url: ApiClient.adminSubscription,
      data: {
        "type" : offer.id,
      },
      bearerToken: PrefUtils.getToken(),
    );

    if (res == null) return null;
    if (res.statusCode != 200) {
      ErrorModel.fromResponse(res);
    } else {
      print(res.data);
      showMessage(res.data[ApiKeys.message]);
    }
    return null;
  }


}
